package 纯数组;

import com.alibaba.fastjson.JSON;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-09-30
 **/

public class No189旋转数组 {

    public void rotate(int[] nums, int k) {
        k %= nums.length;
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k-1);
        reverse(nums, k, nums.length - 1);
    }

    private void reverse(int[] arr, int index1, int index2) {

        while (index1 < index2) {
            int temp = arr[index1];
            arr[index1] = arr[index2];
            arr[index2] = temp;
            index1++;
            index2--;
        }

    }

    public static void main(String[] args) {
        No189旋转数组 n = new No189旋转数组();
        int[] arr = {1,2};
        n.rotate(arr, 7);
        System.out.println(JSON.toJSONString(arr));
    }

}
